package cn.com.dhc.danlu.widget.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yiji.www.frameworks.utils.DensityUtils;

import java.math.BigDecimal;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CalculatePriceUtil;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.holder.EnvelopHolder;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetMyCouponListResponse.MyCouponListModel;

/**
 * @author wuhaiyang 红包状态（未使用、已过期、已使用）适配器
 */
public class EnvelopStatusAdapter extends MyBaseAdapter<String> {

    public EnvelopStatusAdapter (Context context) {
        super (context);
    }

    @SuppressLint ("SetTextI18n")
    @Override
    protected View createView (int position, View convertView, ViewGroup parent) {
        EnvelopHolder holder;
        MyCouponListModel model = (MyCouponListModel) getItem (position);
        String status = model.getCouponStatus ();
        if (null == convertView) {
            convertView = mInflater.inflate (R.layout.item_envelop_status, parent, false);
            holder = new EnvelopHolder (convertView);

            switch (status) {
                case "01":
                    // 1-未使用
                    holder.iv_use_icon.setVisibility (View.GONE);
                    holder.tv_dayleft.setVisibility (View.VISIBLE);
                    break;
                case "04":
                    //2-已过期
                    holder.tv_dayleft.setVisibility (View.GONE);
                    holder.iv_use_icon.setVisibility (View.VISIBLE);
                    holder.iv_use_icon.setImageResource (R.drawable.icon_overdue);
                    holder.tv_envelop_timeornum.setGravity (Gravity.LEFT | Gravity.CENTER_VERTICAL);
                    holder.tv_envelop_timeornum.setPadding (DensityUtils.dip2px (mInflater.getContext (), 20), 0, 0, 0);
                    holder.iv_coupon_bg.setImageResource (R.drawable.icon_nouse_coupon_bg4);
                    break;
                case "02":
                    //3-已使用
                    holder.tv_dayleft.setVisibility (View.GONE);
                    holder.iv_use_icon.setVisibility (View.VISIBLE);
                    holder.iv_use_icon.setImageResource (R.drawable.icon_hasuse);
                    holder.iv_coupon_bg.setImageResource (R.drawable.icon_nouse_coupon_bg4);
                    break;
            }

            convertView.setTag (holder);
        } else {
            holder = (EnvelopHolder) convertView.getTag ();
        }
        holder.tv_money.setText ("￥" + CalculatePriceUtil.converseAmount (CommonUtils.fenToYuan (new BigDecimal (model.getCouponAmt ())).toPlainString ()));
        holder.tv_dayleft.setText ("还有" + model.getTimeLeft () + "天过期");
        holder.tv_envelop_title.setText (model.getCouponName ());
        if (status.equals ("02")) {
            //已经使用
            List<String> orderList = model.getOrderList ();
            if (!orderList.isEmpty ()) {
                if (orderList.size () == 1) {
                    holder.tv_envelop_num.setText ("订单号：" + orderList.get (0));
                    holder.tv_type_limit.setVisibility (View.GONE);
                } else {
                    holder.tv_type_limit.setVisibility (View.VISIBLE);
                    if (orderList.size () == 2) {
                        holder.tv_envelop_num.setText ("订单号：" + orderList.get (0));
                        holder.tv_type_limit.setText ("订单号：" + orderList.get (1));
                    } else {
                        holder.tv_envelop_num.setText ("订单号：" + orderList.get (0));
                        holder.tv_type_limit.setText ("订单号：" + orderList.get (1) + "\n ......");
                    }
                }
            } else {
                holder.tv_envelop_num.setText ("未使用订单");
                holder.tv_type_limit.setVisibility (View.GONE);
            }
            holder.tv_envelop_timeornum.setText ("红包编号：" + model.getCouponCode ());
        } else {
            holder.tv_envelop_num.setText ("红包编号：" + model.getCouponCode ());
            String limitStr = "品类限制：";
            if (!TextUtils.isEmpty (model.getKindsLimit ())) {
                limitStr += model.getKindsLimit ();
            } else {
                limitStr += "全场通用";
            }
            holder.tv_type_limit.setText (limitStr);
            holder.tv_envelop_timeornum.setText ("有效期至：" + model.getExpireDay ());
        }
        if (status.equals ("01")) {
            showDiffrentBg (holder.iv_coupon_bg, model.getKindsLimit ());
        }
        return convertView;
    }

    private void showDiffrentBg (ImageView iv_coupon_bg, String kindsLimit) {
        if (TextUtils.isEmpty (kindsLimit)) {//全场通用
            iv_coupon_bg.setImageResource (R.drawable.icon_nouse_coupon_bg2);
        } else if (kindsLimit.contains ("洋酒")) {//洋酒
            iv_coupon_bg.setImageResource (R.drawable.icon_nouse_coupon_bg1);
        } else if (kindsLimit.contains ("白酒")) {//白酒
            iv_coupon_bg.setImageResource (R.drawable.icon_nouse_coupon_bg3);
        } else if (kindsLimit.contains ("葡萄酒")) {// 葡萄酒
            iv_coupon_bg.setImageResource (R.drawable.icon_nouse_coupon_bg6);
        } else {//其他饮品
            iv_coupon_bg.setImageResource (R.drawable.icon_nouse_coupon_bg5);
        }
    }
}